package simulationhandlers;

import creators.Creator;
import creators.MassCreator;
import creators.SpringCreator;
import forces.CenterOfMass;
import forces.Gravity;
import forces.Viscosity;
import forces.WallForce;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * Reads and processes the model file.
 * @author Robert C. Duvall
 */

public class Factory {
    private String myMassString = "mass";
    private String mySpringString = "spring";
    private String myGravityString = "gravity";
    private String myViscosityString = "viscosity";
    private String myCentermassString = "centermass";
    private String myWallString = "wall";
    private ArrayList<Creator> myAllPossibleCreators;
    /**
     *
     * @param sim 
     * @param modelFile 
     */
    public void loadModel (Simulation sim, File modelFile) {
        try {
            Scanner input = new Scanner(modelFile);
            boolean isAssemblyFile = false;
            int assemblyNumber = sim.getNumberOfAssemblies();
            constructPossibleCreatorsList(sim, assemblyNumber);

            // indices of current assemblies are at most assemblyNum-1, so
            // assemblyNum is the index for the new assembly
            while (input.hasNext()) {
                Scanner line = new Scanner(input.nextLine());
                if (line.hasNext()) {
                    String type = line.next();
                    // creates a new assembly for the masses and springs in this
                    // data file
                    if (myMassString.equals(type) || mySpringString.equals(type)) {
                        if (!isAssemblyFile) {
                            isAssemblyFile = true;
                         // creates lists to hold the
                            // masses and springs
                            // for the new assembly
                            sim.addAssembly();
                        }
                    }
                    addSimulationElements(type, sim, assemblyNumber, line);
                }
            }
            sim.addGlobalForces();
            input.close();
        }
        catch (FileNotFoundException e) {
            // should not happen because File came from user selection
            sim.removeAssembly(sim.getNumberOfAssemblies() - 1);
            e.printStackTrace();
        }
    }
    /**
     *
     * @param type 
     * @param sim 
     * @param assemblyNumber 
     * @param line 
     */
    public void addSimulationElements(String type, Simulation sim, int assemblyNumber, 
            Scanner line) {

        for (Creator c : myAllPossibleCreators) {
            if (c.getName().equals(type)) {
                c.addMass(line);
            }
        }
        if (myGravityString.equals(type)) {
            sim.modifyForce(new Gravity(line.nextDouble(), line.nextDouble()));
        }
        else if (myViscosityString.equals(type)) {
            sim.modifyForce(new Viscosity(line.nextDouble()));
        }
        else if (myCentermassString.equals(type)) {
            sim.modifyForce(new CenterOfMass(line.nextInt(), line.nextDouble()));
        }
        else if (myWallString.equals(type)) {
            sim.modifyForce(new WallForce(line.nextInt(), line.nextDouble(), 
                    line.nextDouble(), sim));
        }

    }
    /**
     * 
     * @param sim 
     * @param assemblyNumber 
     */
    public void constructPossibleCreatorsList(Simulation sim, int assemblyNumber) {
        myAllPossibleCreators = new ArrayList<Creator>();
        myAllPossibleCreators.add(new MassCreator(sim, assemblyNumber));

        myAllPossibleCreators.add(new SpringCreator(sim, assemblyNumber));
    }
}
